package com.bocai.kelalacar.ui.car.set.presenter;

import android.util.Log;

import com.bocai.kelalacar.base.BaseResponse;
import com.bocai.kelalacar.bean.UploadImgBean;
import com.bocai.kelalacar.bean.UserBean;
import com.bocai.kelalacar.net.Api;
import com.bocai.kelalacar.ui.car.set.contract.SetContract;
import com.bocai.kelalacar.util.AESUtils;
import com.bocai.kelalacar.util.RetryWithDelay;
import com.google.gson.Gson;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yuanfei on 2017/6/20.
 */

public class SetPresenter extends SetContract.presenter {
    private final SetContract.view view;
    private final Api api;
    public String start = "{\"content\":";
    public String end = "}";

    @Inject
    public SetPresenter(SetContract.view view, Api api) {
        this.view = view;
        this.api = api;
    }
    @Override
    public void AvatarImg(String memberId, String AvatarId) {
        api.avatarImg(memberId,AvatarId).subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable",e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                view.stopLoading();
                if (baseResponse.getReturnCode().equals("200")){
//                    view.AvatarImgSuccess();
//                    String result = start+ AESUtils.decode(baseResponse.getData())+end;
//                    Log.e("result",result);
                    view.AvatarImgSuccess();
                }else{
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }

    @Override
    public void uploadImg(List<String> photos) {
        view.showLoading("正在上传...");
        List<MultipartBody.Part> parts = new ArrayList<>();
        MultipartBody.Builder builder=new MultipartBody.Builder();
        Map<String, RequestBody> map = new HashMap<>();
        for (int i = 0;i < photos.size();i++){
            File file = new File(photos.get(i));
            map.put("file"+"\";filename =\""+ file.getName(), RequestBody.create(MediaType.parse("multipart/form-data"),file));
            RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"),file);
            builder.addFormDataPart("file",file.getName(),requestBody);
            parts.add(builder.build().part(i));
        }
        api.uploadImage(parts).subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable",e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                view.stopLoading();
                Log.e("baseResponse",baseResponse.toString());
                Log.e("baseResponse",baseResponse.getReturnCode().equals("200")+"");
                if (baseResponse.getReturnCode().equals("200")){
                    String result = start+ AESUtils.decode(baseResponse.getData())+end;
                    Log.e("result",result);
                    UploadImgBean uploadImgBean = new Gson().fromJson(result,UploadImgBean.class);
                    view.uploadImgSuccess(uploadImgBean);
                }else{
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }

    @Override
    public void logOut() {
        view.showLoading("");
        api.logOut().subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable",e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                view.stopLoading();
                if (baseResponse.getReturnCode().equals("200")){
                    view.logOutSuccess();
                }else if (baseResponse.getReturnCode().equals("401")){
                    view.logOutSuccess();
                }
                else{
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }

    @Override
    public void getUser() {
        view.showLoading("");
        api.getUser().subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable",e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                view.stopLoading();
                if (baseResponse.getReturnCode().equals("200")){
                    String result = AESUtils.decode(baseResponse.getData());
                    Log.e("result",result);
                    UserBean userBean = new Gson().fromJson(result,UserBean.class);
                    view.getUser(userBean);
                }else{
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }
}
